package com.example.x1c.sharedpreferences;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();

                editor.putString("name", "胡国栋");
                editor.putBoolean("married", true);
                editor.putInt("age", 30);
                editor.apply();

            }
        });

        Button bn_restore = findViewById(R.id.bn_restore);

        bn_restore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
                String name = pref.getString("name","");
                int age = pref.getInt("age", 0);
                Log.d(TAG, "onClick: name : " + name +", age : "+age);

            }
        });
    }
}
